<?php
namespace app\home\controller;

// 引入TP框架的控制器
use think\Controller;


class Index extends Controller
{
    public function __construct()
    {
        // 继承父类
        parent::__construct();

        // 在构造函数中 保存一个模型
        $this->BusinessModel = model('Business.Business');
        $this->SubjectModel = model('Subject.Subject');
        $this->CategoryModel = model('Subject.Category');
    }

    public function index()
    {
        $categoryList = [];
        $categoryList = $this->CategoryModel->all();
        // dump(array_column($categoryList,'id'));
        // echo ;
        // exit;
        $sublist = $this->SubjectModel->limit(5)->select();
        $subjectList = $this->SubjectModel->select();
        // dump($subjectList);
        // exit;
        foreach ($categoryList as $item) {
            $list = $this->SubjectModel->where('cateid', $item['id'])->limit(2)->select();
            $item['subject'] = $list;
            // dump($subjectList);
        }
        // exit;
        $this->assign([
            'list'=>$sublist,
            'subjectList' => $subjectList,
            'categoryList' => $categoryList,
        ]);

        //加载视图==模板 /application/home/view/index/index.html
        return $this->view->fetch();
    }
    public function register()
    {
        // 关闭模板布局
        $this->view->engine->layout(false);

        //先判断是否有Post表单过来
        if ($this->request->isPost()) {
            //接收全部参数
            // $all = $this->request->param();

            // 接受参数
            $mobile = $this->request->param('mobile', '', 'trim');
            $password = $this->request->param('password', '', 'trim');

            if (empty($password)) {
                $this->error('密码不能为空');
                exit;
            }
            $reg = "/\w{6,}/";
            if (!preg_match($reg, $password)) {
                $this->error('密码不能小于6位');
                exit;
            }

            // 生成一个密码盐
            $salt = build_ranstr();

            // 密码加密
            $password = md5($password . $salt);


            // 生成一个邀请码
            // $invitecode = build_ranstr();

            // 组装数据
            $data = [
                'mobile' => $mobile,
                'nickname' => $mobile,
                'salt' => $salt,
                'password' => $password,
                'money' => 0,
                'auth' => 0,
                //认证状态
                'deal' => 0, //成交状态
            ];

            // 查询来源记录
            $source = model('Business.Source')->where(['name' => ['LIKE', '%云课堂%']])->find();

            if ($source) {
                $data['sourceid'] = $source['id'];
            }

            //对数据做验证，验证通过后，就插入到数据库里面
            //会先去home里面找，找不到就去common模块里面去找
            $result = $this->BusinessModel->validate('common/business/Business')->save($data);

            // 插入失败
            if ($result === FALSE) {
                // 错误信息可以让模型返回出来
                $this->error($this->BusinessModel->getError());
                exit;
            } else {
                // 插入成功,跳转到登录界面
                $this->success('注册成功', url('home/index/login'));
                exit;
            }
        }
        // application/home/view/index/register.html
        return $this->view->fetch();
    }

    // 登录方法
    public function login()
    {
        // 关闭模板布局
        $this->view->engine->layout(false);

        if ($this->request->isPost()) {
            // session_start();
            $mobile = $this->request->param('mobile', '', 'trim');
            $password = $this->request->param('password', '', 'trim');

            //查询用户是否存在
            $business = $this->BusinessModel->where(['mobile' => $mobile])->find();

            if (!$business) {
                $this->error('用户不存在');
                exit;
            }
            $salt = $business['salt'];
            $pwd = $business['password'];

            if ($pwd !== md5($password . $salt)) {
                $this->error('密码不正确');
                exit;
            }
            //缓存cookie
            $cache = [
                'id' => $business['id'],
                'mobile' => $business['mobile'],
                'avatar' => $business['avatar'],
            ];

            // 保存cookie信息
            cookie('business', $cache);
            $this->redirect(url('home/business/index'));
            exit;
        }
        // application/home/view/index/login.html
        return $this->view->fetch();
    }

    // 退出登录
    public function logout()
    {
        // 删除cookie
        cookie('business', null);
        $this->redirect(url('home/index/login'));
        exit;
    }

}